# -*- coding: utf-8 -*-

import sys
import os
import pyRXP
import pprint
import time
import math

import re

class DotGraph:
    def __init__(self):
        
        self.width = None
        self.height = None
        
        self.title = ""
        self.nodes_dict = {}
        self.edges_dict = {}
        self.nodes = []
        self.edges = []
        self.isModified = False
        
        
    def __str__(self):
        return self.json()
        
    def dotGen(self):
        dot =  """
digraph G {
    rankdir=TB;
    node [shape=record]; 
        """
        
        for node in self.nodes:
            dot = dot +"%s;\n" %( node )
            #print "[%s,%s,%s,%s]," %(node.x, node.y, node.width, node.height)
        for edge in self.edges:
            dot = dot +"%s;\n" %( edge )
            print self.nodes_dict[edge.nodefrom], self.nodes_dict[edge.nodeto]
            print edge.pathd
        dot = dot + "}";
        return dot

    def json(self):
        dot =  "{"
        
        for node in self.nodes:
            dot = dot + "{%s:[(%s,%s),(%s,%s)]},\n" %(node.name, node.x, node.y, node.width, node.height)
        for edge in self.edges:
            #dot = dot + "%s,%s" %( self.nodes_dict[edge.nodefrom], self.nodes_dict[edge.nodeto])
            dot = dot + "{(%s,%s):" %(edge.nodefrom, edge.nodeto)
            dot = dot + "%s}\n" %(edge.pathd)
        dot = dot +"}"
        return dot

    def addNode(self, node):
        self.nodes_dict[node.name]  = node
        self.nodes.append(node)

        
    def addEdge(self, edge):
        self.edges.append(edge)


"""
"""
class Node:
    def __init__(self, name, points):
        self.name = name
        self.type = "record"
        self.id = None
        self.points = points
        self.og = None
        
        
        
        """
        self.p1
        self.p2
        self.p3
        self.p4
        """
        self.createdon = time.time()
        self.modifiedon = None
        self.width = 0
        self.height = 0
        self.x = 0
        self.y = 0
        self.isModified = False
        self.setPara(points)
        

    def setPara(self, points):
        #print points
        self.width  = math.fabs(points[0][0] - points[2][0])
        #print min(points[1][1], points[0][1])
        self.height = math.fabs(points[0][1] - points[1][1])
        if self.height == 0:
            self.height =40.0
        #print min(points[1][0], points[2][0])
        #print self.width
        #print self.height
        self.x =  min(points[0][0] , points[2][0]) + (self.width)/2
        self.y =  min(points[0][1] ,points[1][1]) + (self.height)/2
        
        
    def __str__(self):
        return " %s [label=\"%s\"]" %(self.name, self.name)
        
    def setPoints(self, points):
        pass


class RecordNode(Node):
    def __init__(self):
        pass

class EllispNode(Node):
    def __init__(self):
        pass

"""
"""

class Edge:
    def __init__(self, name, pathd):
        self.name = name
        self.id = None
        self.path = []
        self.pathd = pathd
        self.nodefrom = ""
        self.nodeto  = ""
        self.setNodes()
        self.createdon = time.time()
        self.modifiedon = None
        self.isModified = False
        
    def __str__(self):
        return self.name
        
    def setNodes(self)    :
        (self.nodefrom, self.nodeto) =  self.name.split('->')
        
